package com.helloandroid.models
//
//import androidx.lifecycle.MutableLiveData
//import androidx.lifecycle.ViewModel
//import androidx.lifecycle.viewModelScope
//import com.github.salomonbrys.kotson.fromJson
//import com.github.salomonbrys.kotson.jsonObject
//import com.google.gson.JsonElement
//import com.helloandroid.MyApplication
//import com.helloandroid.tools.GUtils
//import com.helloandroid.tools.MyLog
//import com.helloandroid.tools.NetWorkUtil
//import kotlinx.coroutines.Dispatchers
//import kotlinx.coroutines.async
//import kotlinx.coroutines.launch
//
//
//data class TXRecord(val time:Long,val money:Int)
//
//class TXRecordViewModel : ViewModel() {
//
//    val records:MutableLiveData<ArrayList<TXRecord>> = MutableLiveData()
//
//    fun getRecords(callback:(Boolean)->Unit){
//        if (!NetWorkUtil.networkConnected()){
//            AppUtil.toast("请打开网络!")
//            callback(false)
//            return
//        }
//        viewModelScope.launch {
//            val defer = async(Dispatchers.IO){
//                httpGetRecords()
//            }
//            val jsonStr = defer.await()
//            MyLog.elog("服务端返回: $jsonStr")
//            val gson = GUtils.getGson()
//            val jsonObj = (gson.fromJson(jsonStr) as JsonElement).asJsonObject
//            val err = jsonObj[GUtils.KeyErr].asInt
//            if(err == 0){
//                val lstRecord = gson.fromJson<ArrayList<TXRecord>>(jsonObj[GUtils.KeyData].asString)
//                records.value = lstRecord
//                callback(true)
//            }else{
//                MyLog.elog("err:$err,msg:${jsonObj[GUtils.KeyMsg].asString}")
//                callback(false)
//            }
//        }
//    }
//
//    private suspend fun httpGetRecords():String{
//        NetWorkUtil.mockDelay()
//
//        val curTime = MyApplication.sysTime
//
//        val lst = arrayListOf<TXRecord>(
//                TXRecord(curTime - 10 * GUtils.OneDayMs - 2 * GUtils.TwoHourMs,3000),
//                TXRecord(curTime - 3 * GUtils.OneDayMs - 7 * GUtils.TwoHourMs,2000),
//                TXRecord(curTime - 3 * GUtils.OneDayMs - 8 * GUtils.TwoHourMs,1000),
//                TXRecord(curTime - 3 * GUtils.OneDayMs - 3 * GUtils.TwoHourMs,5000),
//        )
//        return  jsonObject(
//                "err" to 0,
//                "data" to GUtils.getGson().toJson(lst)
//        ).toString()
//    }
//}